package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.List;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * This method returns the fuel type of a source (non-virtual) or a list of sub-sources fuel type
 * from a virtual source. The three types of fuel sources identified in method are LSFO, Waste, and
 * Coal. This class extends CommandLineInterface to have access to the WattDepotClient and
 * implements WattDepotCliCommand interface to ensure this class does a specific command.
 * 
 * @author Edward Meyer, Bao Huy Ung
 * 
 */
public class FuelType extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Constructor for FuelType class that will list all sources using a type of fuel.
   */
  public FuelType() {
    // empty constructor.
  }

  /**
   * Returns the requested a list of source(list of non-virtual sources) fuel types.
   * 
   * @param args is the list of arguments entered by the user.
   * @return returns the concatenated list of soureces in their respective fuel types.
   */
  public String doCommand(List<String> args) {

    // check if there are too many/little arguments
    if (args.size() != 2) {
      return "Error: Command contains extraneous arguments at the end.";
    }

    String inputSourceName = args.get(1);
    Source inputSource = null;

    // Check if a valid source was entered.
    inputSource = isValidSource(inputSourceName);
    if (inputSource == null) {
      return "\"" + inputSourceName + "\" is not a valid source.";
    }

    List<Source> sources = new ArrayList<Source>();

    // Use recursive getNonVirtuals method defined in CommandLineInterface get a list of sources
    if (inputSource.isVirtual()) {
      try {
        sources = getNonVirtuals(inputSource, sources);
      }
      // If anything goes wrong during the recursion process getNonVirtuals.
      catch (Exception e) {
        return "Error: Could not compile a list of all non-virtual subsources for source \""
            + inputSourceName + "\".\n" + e;
      }
    }
    // If source is already a nonVirutal just add it to the list
    else if (!inputSource.isVirtual()) {
      sources.add(inputSource);
    }

    String fuelLSFO = "";
    String fuelWaste = "";
    String fuelCoal = "";
    String fuelType = "";
    // concatenate all sources of the same type together
    for (Source subSource : sources) {
      fuelType = subSource.getProperty("fuelType");

      if (fuelType.equalsIgnoreCase("lsfo")) {
        fuelLSFO = fuelLSFO.concat(subSource.getName() + ", ");
      }
      else if (fuelType.equalsIgnoreCase("waste")) {
        fuelWaste = fuelWaste.concat(subSource.getName() + ", ");
      }
      else if (fuelType.equalsIgnoreCase("coal")) {
        fuelCoal = fuelCoal.concat(subSource.getName() + ", ");
      }
    }
    return "LSFO : " + fuelLSFO + "\n" + "Waste : " + fuelWaste + "\n" + "Coal : " + fuelCoal;

  }

}
